package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DealCommonService {
    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private UserService userService;

    @Autowired
    private DealParamService dealParamService;

    @Autowired
    private ShareholderBuyGrantHistoryService shareholderBuyGrantHistoryService;

    @Autowired
    private AliveCoinActivateHistoryService aliveCoinActivateHistoryService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private HangBuyService hangBuyService;

    @Autowired
    private HangSaleService hangSaleService;

    @Autowired
    private ShareholderCoinGrantHistoryService shareholderCoinGrantHistoryService;

    @Autowired
    private UserCommonService userCommonService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private FeeService feeService;

    public SurveyList getSurveyList() {
        SurveyList surveyList = new SurveyList();
        Integer totalHangBuyOrder = hangBuyService.getTotalCount();
        //
        surveyList.setTotalHangBuyOrder(totalHangBuyOrder);
        Integer todayHangBuyOrder = hangBuyService.getTodayCount();
        //
        surveyList.setTodayHangBuyOrder(todayHangBuyOrder);
        List<HangBuy> hangBuys = hangBuyService.getAllWithoutComplete();
        BigDecimal totalHangBuyCount = new BigDecimal(0);
        for (HangBuy hangBuy : hangBuys) {
            BigDecimal aliveCoinCount = hangBuy.getAliveCoinCount();
            totalHangBuyCount = totalHangBuyCount.add(aliveCoinCount);
        }
        //
        surveyList.setTotalHangBuyCount(totalHangBuyCount);
        List<HangBuy> todayHangBuys = hangBuyService.getByToday();
        BigDecimal todayHangBuyCount = new BigDecimal(0);
        for (HangBuy hangBuy : todayHangBuys) {
            BigDecimal aliveCoinCount = hangBuy.getAliveCoinCount();
            todayHangBuyCount = todayHangBuyCount.add(aliveCoinCount);
        }
        //
        surveyList.setTodayHangBuyCount(todayHangBuyCount);
        Integer totalHangSaleOrder = hangSaleService.getTotalCount();
        surveyList.setTotalHangSaleOrder(totalHangSaleOrder);
        Integer todayHangSaleOrder = hangSaleService.getTodayCount();
        surveyList.setTodayHangSaleOrder(todayHangSaleOrder);
        BigDecimal totalHangSaleCount = new BigDecimal(0);
        BigDecimal todayHangSaleCount = new BigDecimal(0);
        List<HangSale> totalHangSale = hangSaleService.getAll();
        List<HangSale> todayHangSale = hangSaleService.getByToday();
        for (HangSale hangSale : totalHangSale) {
            totalHangSaleCount = totalHangSaleCount.add(hangSale.getAliveCoinCount());
        }
        surveyList.setTotalHangSaleCount(totalHangSaleCount);
        for (HangSale hangSale : todayHangSale) {
            todayHangSaleCount = todayHangSaleCount.add(hangSale.getAliveCoinCount());
        }
        surveyList.setTodayHangSaleCount(todayHangSaleCount);


        Integer totalDealOrder = userDealHistoryService.getTotalCount();
        Integer todayDealOrder = userDealHistoryService.getTodayCount();
        surveyList.setTotalDealOrder(totalDealOrder);
        surveyList.setTodayDealOrder(todayDealOrder);
        BigDecimal totalDealCount = new BigDecimal(0);
        BigDecimal todayDealCount = new BigDecimal(0);

        List<UserDealHistory> totalDealHistory = userDealHistoryService.getSurveyDealHistory();
        List<UserDealHistory> todayDealHistory = userDealHistoryService.getTodaySurveyDealHistory();

        for (UserDealHistory dealHistory : totalDealHistory) {
            BigDecimal dealAliveCoinCount = dealHistory.getDealAliveCoinCount();
            if (dealAliveCoinCount.compareTo(new BigDecimal(0))<0){
                dealAliveCoinCount =  dealAliveCoinCount.multiply(new BigDecimal(-1));
            }
            totalDealCount = totalDealCount.add(dealAliveCoinCount);
        }
        totalDealCount = totalDealCount.divide(new BigDecimal(2), 8, BigDecimal.ROUND_HALF_UP);
        surveyList.setTotalDealCount(totalDealCount);
        for (UserDealHistory dealHistory : todayDealHistory) {
            BigDecimal dealAliveCoinCount = dealHistory.getDealAliveCoinCount();
            if (dealAliveCoinCount.compareTo(new BigDecimal(0))<0){
                dealAliveCoinCount =  dealAliveCoinCount.multiply(new BigDecimal(-1));
            }
            todayDealCount = todayDealCount.add(dealAliveCoinCount);
        }
        todayDealCount = totalDealCount.divide(new BigDecimal(2), 8, BigDecimal.ROUND_HALF_UP);
        surveyList.setTodayDealCount(todayDealCount);
        List<UserDealHistory> totalBackHistory = userDealHistoryService.getDealHistoryByDealScene(DealScene.SYSTEM_BUY_BACK);
        List<UserDealHistory> todayBackHistory = userDealHistoryService.getByTodayAndDealScene(DealScene.SYSTEM_BUY_BACK);
        Integer totalBackOrder = totalBackHistory.size();
        surveyList.setTotalBackOrder(totalBackOrder);
        Integer todayBackOrder = todayBackHistory.size();
        surveyList.setTodayBackOrder(todayBackOrder);
        BigDecimal totalBackCount = new BigDecimal(0);
        BigDecimal todayBackCount = new BigDecimal(0);
        for (UserDealHistory dealHistory : totalBackHistory) {
            totalBackCount = totalBackCount.add(dealHistory.getDealAliveCoinCount());
        }
        surveyList.setTotalBackCount(totalBackCount);
        for (UserDealHistory dealHistory : todayBackHistory) {
            todayBackCount = todayBackCount.add(dealHistory.getDealAliveCoinCount());
        }
        surveyList.setTodayBackCount(todayBackCount);
        Integer totalSealupCount = userCommonService.getTotalSealupCount();
        Integer totalFrozenCount = userCommonService.getTotalFrozenCount();
        Integer todaySealUpCount = userCommonService.getByTodayAndUserState(UserState.CLOSED).size();
        Integer todayFrozenCount = userCommonService.getByTodayAndUserState(UserState.FROZEN).size();
        surveyList.setTotalSealupCount(totalSealupCount);
        surveyList.setTodaySealupCount(todaySealUpCount);
        surveyList.setTotalFrozenCount(totalFrozenCount);
        surveyList.setTodayFrozenCount(todayFrozenCount);

        Integer unactiveCount = userRoleService.getsByUserRole(UserType.NONE).size();
        Integer activedCount = userRoleService.getsByUserRole(UserType.MEMBER).size();
        Integer totalRegisterCount = unactiveCount + activedCount;
        surveyList.setTotalRegisterCount(totalRegisterCount);
        surveyList.setActivedCount(activedCount);
        surveyList.setUnactiveCount(unactiveCount);

//        int dealThreeMonthCount = userCommonService.getNoDealUserByMonth(3).size();
//        int dealOneMonthCount = userCommonService.getNoDealUserByMonth(1).size();
//        int dealSevenDayCount = userCommonService.getNoDealUserByDay(7).size();
//        Integer noDealThreeMonthCount = totalRegisterCount - dealThreeMonthCount;
//        Integer noDealOneMonthCount = totalRegisterCount - dealOneMonthCount;
//        Integer noDealSevenDayCount = totalRegisterCount - dealSevenDayCount;


        Integer noDealThreeMonthCount = userCommonService.getNoDealUserByMonth(3).size();
        Integer noDealOneMonthCount = userCommonService.getNoDealUserByMonth(1).size();
        Integer noDealSevenDayCount = userCommonService.getNoDealUserByDay(7).size();
        surveyList.setNoDealOneMonthCount(noDealOneMonthCount);
        surveyList.setNoDealThreeMonthCount(noDealThreeMonthCount);
        surveyList.setNoDealSevenDayCount(noDealSevenDayCount);


        return surveyList;
    }

    public DivList getDivList(@RequestParam(defaultValue = "1", value = "coinTypeId") int coinTypeId) {
        DivList divList = new DivList();
        BigDecimal todaySleepCoinDividends = new BigDecimal(0);//
        BigDecimal todayAliveCoinDividends = new BigDecimal(0);

        //今日股东休眠币
        List<ShareholderCoinGrantHistory> todaySleepCoinList = shareholderCoinGrantHistoryService.getTodayByCoinTypeIdAndType(coinTypeId, Type.SLEEPCOIN);
        for (ShareholderCoinGrantHistory shareholderBuyGrantHistory : todaySleepCoinList) {
            BigDecimal count = shareholderBuyGrantHistory.getCount();
            todaySleepCoinDividends = todaySleepCoinDividends.add(count);
        }

        divList.setTodaySleepCoinDividends(todaySleepCoinDividends);
        //今日股东活币配发数量 = 今日购买+(今日活币配发)
        List<ShareholderCoinGrantHistory> todaySysList = shareholderCoinGrantHistoryService.getTodayByCoinTypeIdAndType(coinTypeId, Type.ALIVECOIN);
        List<ShareholderBuyGrantHistory> todayBuyAliveList = shareholderBuyGrantHistoryService.getTodayByCoinTypeIdAndType(coinTypeId, Type.ALIVECOIN);
        if (todaySysList == null || todaySysList.isEmpty()) {
            todayAliveCoinDividends = new BigDecimal(0);
            for (ShareholderBuyGrantHistory shareholderBuyGrantHistory : todayBuyAliveList) {
                BigDecimal count = shareholderBuyGrantHistory.getCount();
                todayAliveCoinDividends = todayAliveCoinDividends.add(count);
            }
        } else {
            BigDecimal todayHaveDivdends = shareholderBuyGrantHistoryService.getTodayHaveDivdends(coinTypeId);
            todayAliveCoinDividends = todayAliveCoinDividends.add(todayHaveDivdends);
        }

        divList.setTodayAliveCoinDividends(todayAliveCoinDividends);
        //累计货币配发总量、(已经购买的活币 + 最近7次的配发总量 + 休眠币数量  )
        //今日货币配发总量 (今日已经购买的活币+今日配发 + 休眠币 )
        BigDecimal totalSleepCoinDividends = new BigDecimal(0);
        BigDecimal totalAliveCoinDividends = new BigDecimal(0);
        BigDecimal totalHaveDivdends = shareholderBuyGrantHistoryService.getTotalHaveDivdends(coinTypeId);
        totalAliveCoinDividends = totalAliveCoinDividends.add(totalHaveDivdends);
        divList.setTotalAliveCoinDividends(totalAliveCoinDividends);
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistories = shareholderCoinGrantHistoryService.getsByCoinTypeIdAndType(coinTypeId, Type.SLEEPCOIN);
        for (ShareholderCoinGrantHistory shareholderCoinGrantHistory : shareholderCoinGrantHistories) {
            BigDecimal count = shareholderCoinGrantHistory.getCount();
            totalSleepCoinDividends = totalSleepCoinDividends.add(count);
        }
        divList.setTotalSleepCoinDividends(totalSleepCoinDividends);

        BigDecimal totalSysCount = new BigDecimal(0);
        BigDecimal todaySysCount = new BigDecimal(0);
        totalSysCount = totalSysCount.add(totalAliveCoinDividends).add(totalSleepCoinDividends);
        todaySysCount = todaySysCount.add(todayAliveCoinDividends).add(todaySleepCoinDividends);
        divList.setTotalSysCount(totalSysCount);
        divList.setTodaySysCount(todaySysCount);
        //累计股东活币释放量、今日股东活币释放量
        BigDecimal totalAliveRelease = new BigDecimal(0);
        BigDecimal todayAliveRelease = new BigDecimal(0);
        // TODO: 2019/6/2 这里的alive_coin_activate_history表应该改为user_deal_history表
        List<AliveCoinActivateHistory> totalAliveByCoinTypeIdAndType = aliveCoinActivateHistoryService.getsByCoinTypeAndType(coinTypeId, Type.ALIVECOIN);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : totalAliveByCoinTypeIdAndType) {
            BigDecimal count = aliveCoinActivateHistory.getCount();
            totalAliveRelease = totalAliveRelease.add(count);
        }
        List<AliveCoinActivateHistory> todayAliveByCoinTypeIdAndType = aliveCoinActivateHistoryService.getTodayByCoinTypeIdAndType(coinTypeId, Type.ALIVECOIN);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : todayAliveByCoinTypeIdAndType) {
            BigDecimal count = aliveCoinActivateHistory.getCount();
            todayAliveRelease = todayAliveRelease.add(count);
        }
        divList.setTotalAliveRelease(totalAliveRelease);
        divList.setTodayAliveRelease(todayAliveRelease);

        //累计股东休眠币释放量、今日股东休眠币释放量
        BigDecimal totalSleepRelease = new BigDecimal(0);
        BigDecimal todaySleppRelease = new BigDecimal(0);
        List<AliveCoinActivateHistory> totalSleepbyCoinTypeIdAndTypeExceptSuperManager = aliveCoinActivateHistoryService.getsByCoinTypeAndType(coinTypeId, Type.SLEEPCOIN);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : totalSleepbyCoinTypeIdAndTypeExceptSuperManager) {
            BigDecimal count = aliveCoinActivateHistory.getCount();
            totalSleepRelease = totalSleepRelease.add(count);
        }
        List<AliveCoinActivateHistory> todaySleepByCoinTypeIdAndType = aliveCoinActivateHistoryService.getTodayByCoinTypeIdAndType(coinTypeId, Type.SLEEPCOIN);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : todaySleepByCoinTypeIdAndType) {
            BigDecimal count = aliveCoinActivateHistory.getCount();
            todaySleppRelease = todaySleppRelease.add(count);
        }
        divList.setTotalSleepRelease(totalSleepRelease);
        divList.setTodaySleppRelease(todaySleppRelease);

        //新钱包激活所有活币数量
        BigDecimal totalMemberRelease = new BigDecimal(0);
        BigDecimal todayMemberRelease = new BigDecimal(0);
        List<AliveCoinActivateHistory> aliveCoinActivateHistories = aliveCoinActivateHistoryService.getsByCoinType(coinTypeId);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : aliveCoinActivateHistories) {
            totalMemberRelease = totalMemberRelease.add(aliveCoinActivateHistory.getCount());
        }
        List<AliveCoinActivateHistory> todayByCoinTypeId = aliveCoinActivateHistoryService.getTodayByCoinTypeId(coinTypeId);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : todayByCoinTypeId) {
            todayMemberRelease = todayMemberRelease.add(aliveCoinActivateHistory.getCount());
        }
        divList.setTotalMemberRelease(totalMemberRelease);
        divList.setTodayMemberRelease(todayMemberRelease);

        return divList;
    }

    public void updateParam(Integer dealParamId,
                            Integer dealParamKey,
                            String dealParamValue) {
        DealParam dealParam = dealParamService.getByPrimaryKey(dealParamId);
        dealParam.setDealParamKey(dealParamKey);
        dealParam.setDealParamValue(dealParamValue);
        dealParamService.update(dealParam);
    }

    public Boolean checkFeeCount(Integer userId, Integer coinTypeId, Integer dealScene, BigDecimal count) {
        boolean flag = false;
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        Integer userType = user.getType();
        Fee fee = feeService.getFee(userId, coinTypeId, dealScene);
        BigDecimal feeRatio = fee.getFeeRatio();
        BigDecimal baseDealCount = fee.getBaseDealCount();
        UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeIdAndUserType(userId, coinTypeId, userType);
        if (userCapital != null) {
            if (count.compareTo(baseDealCount) > 0) {
                BigDecimal dealCount = count.subtract(baseDealCount);
                BigDecimal feeCount = dealCount.multiply(feeRatio);
                if (feeCount.compareTo(baseDealCount) < 0) {
                    flag = true;
                }
            }
        }
        return flag;
    }

}
